<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-left">
        <div class="left-content">
          <h1>欢迎注册</h1>
          <h2>医疗信息管理系统</h2>
          <p>创建账号，享受全方位的医疗服务体验</p>
        </div>
      </div>
      <div class="register-right">
        <div class="logo-container">
          <el-icon class="logo-icon"><FirstAidKit /></el-icon>
        </div>
        <h2 class="register-title">用户注册</h2>
        <p class="register-subtitle">请填写以下信息完成注册</p>
        
        <el-form 
          ref="formRef"
          :model="registerForm" 
          :rules="registerRules" 
          class="register-form"
        >
          <el-form-item prop="username">
            <el-input 
              v-model="registerForm.username"
              placeholder="请设置用户名"
              prefix-icon="User"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="email">
            <el-input 
              v-model="registerForm.email"
              placeholder="请输入邮箱"
              prefix-icon="Message"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              placeholder="请设置密码"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              placeholder="请确认密码"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="userType">
            <el-select 
              v-model="registerForm.userType" 
              placeholder="请选择用户类型" 
              style="width: 100%"
            >
              <el-option label="患者" value="patient" />
              <el-option label="医生" value="doctor" />
            </el-select>
          </el-form-item>
          
          <div class="register-agreement">
            <el-checkbox v-model="agreement">我已阅读并同意<el-link type="primary" :underline="false">《用户协议》</el-link>和<el-link type="primary" :underline="false">《隐私政策》</el-link></el-checkbox>
          </div>
          
          <el-form-item>
            <el-button 
              type="primary" 
              :loading="loading" 
              class="register-button"
              @click="handleRegister"
              :disabled="!agreement"
            >
              注册
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="register-footer">
          <p>已有账号？ <el-link type="primary" :underline="false" @click="toLogin">立即登录</el-link></p>
          <p class="back-to-home">
            <el-link type="info" :underline="false" @click="backToHome">
              <el-icon><ArrowLeft /></el-icon> 返回首页
            </el-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 注册相关状态
const loading = ref(false)
const agreement = ref(false)
const formRef = ref(null)

// 注册表单
const registerForm = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  userType: 'patient'
})

// 表单验证规则
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.value.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
  userType: [
    { required: true, message: '请选择用户类型', trigger: 'change' }
  ]
}

// 账号注册
const handleRegister = () => {
  if (!formRef.value) return
  
  if (!agreement.value) {
    ElMessage.warning('请阅读并同意用户协议和隐私政策')
    return
  }
  
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        
        // 调用注册API
        const response = await userStore.register({
          username: registerForm.value.username,
          email: registerForm.value.email,
          password: registerForm.value.password,
          role: registerForm.value.userType
        })
        
        ElMessage.success('注册成功，请登录')
        
        // 使用MessageBox确认框告知用户注册成功
        ElMessageBox.alert('您的账号已成功注册，现在可以登录系统', '注册成功', {
          confirmButtonText: '立即登录',
          callback: () => {
            // 注册成功后跳转到登录页
            router.push('/portal/login')
          }
        })
      } catch (error) {
        console.error('注册失败:', error)
        // 显示更具体的错误信息
        if (error.response && error.response.data && error.response.data.message) {
          ElMessage.error(error.response.data.message)
        } else {
          ElMessage.error('注册失败，可能是用户名或邮箱已被使用')
        }
      } finally {
        loading.value = false
      }
    }
  })
}

// 跳转到登录页
const toLogin = () => {
  router.push('/portal/login')
}

// 返回首页
const backToHome = () => {
  router.push('/portal')
}
</script>

<style scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.register-container {
  width: 90%;
  max-width: 1200px;
  min-height: 600px;
  display: flex;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

.register-left {
  flex: 0 0 45%;
  background: linear-gradient(135deg, #1989fa, #0066cc);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.register-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 800"><defs><linearGradient id="a" x1="50%" x2="50%" y1="0%" y2="100%"><stop offset="0%" stop-color="%23ffffff" stop-opacity=".1"/><stop offset="100%" stop-color="%23ffffff" stop-opacity="0"/></linearGradient></defs><circle cx="400" cy="400" r="600" fill="url(%23a)" opacity=".15"/><circle cx="200" cy="600" r="280" fill="url(%23a)" opacity=".15"/><circle cx="650" cy="150" r="200" fill="url(%23a)" opacity=".15"/></svg>');
  background-size: cover;
  opacity: 0.3;
}

.left-content {
  position: relative;
  z-index: 1;
  text-align: left;
}

.left-content h1 {
  font-size: 36px;
  font-weight: 700;
  margin: 0 0 10px;
}

.left-content h2 {
  font-size: 24px;
  font-weight: 500;
  margin: 0 0 30px;
  opacity: 0.9;
}

.left-content p {
  font-size: 16px;
  margin: 0;
  opacity: 0.8;
  line-height: 1.6;
}

.register-right {
  flex: 0 0 55%;
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo-container {
  margin-bottom: 20px;
  background-color: #e6f2ff;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-icon {
  font-size: 30px;
  color: #1989fa;
}

.register-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px;
  text-align: center;
}

.register-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0 0 30px;
  text-align: center;
}

.register-form {
  width: 100%;
  max-width: 380px;
}

.register-form :deep(.el-input__wrapper) {
  padding: 4px 15px;
  height: 44px;
  border-radius: 8px;
}

.register-form :deep(.el-select) {
  width: 100%;
}

.register-form :deep(.el-select__wrapper) {
  padding: 4px 15px;
  height: 44px;
  border-radius: 8px;
}

.register-agreement {
  margin: 15px 0 25px;
  font-size: 14px;
}

.register-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  border-radius: 8px;
}

.register-footer {
  margin-top: 25px;
  text-align: center;
  width: 100%;
  max-width: 380px;
}

.register-footer p {
  margin: 10px 0;
  color: #666;
  font-size: 14px;
}

.back-to-home {
  margin-top: 20px;
}

@media (max-width: 992px) {
  .register-container {
    flex-direction: column;
  }
  
  .register-left {
    flex: 0 0 auto;
    padding: 40px 20px;
  }
  
  .register-right {
    flex: 0 0 auto;
    padding: 40px 20px;
  }
}

@media (max-width: 480px) {
  .register-page {
    padding: 0;
  }
  
  .register-container {
    width: 100%;
    border-radius: 0;
    box-shadow: none;
  }
  
  .register-left {
    padding: 30px 15px;
  }
  
  .register-right {
    padding: 30px 15px;
  }
  
  .left-content h1 {
    font-size: 28px;
  }
  
  .left-content h2 {
    font-size: 20px;
  }
}
</style> 